package architecture.demo6;



import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class FileProcessingApp {
    public static void main(String[] args) {
        selectCodeStyle();
    }

    public static void selectCodeStyle() {
        Scanner scanner = new Scanner(System.in);
        int choice;

        do {
            System.out.println("请选择代码风格：");
            System.out.println("1. 主程序子程序风格");
            System.out.println("2. 面向对象风格");
            System.out.println("3. 事件系统风格");
            System.out.println("4. 管道过滤器风格");
            System.out.println("0. 结束进程");
            System.out.print("请选择: ");

            if (scanner.hasNextInt()) {
                choice = scanner.nextInt();

                switch (choice) {
                    case 0:
                        System.out.println("进程已结束");
                        return;
                    case 1:
                        handleMainSubprogramStyle();
                        break;
                    case 2:
                        handleObjectOrientedStyle();
                        break;
                    case 3:
                        handleEventSystemStyle();
                        break;
                    case 4:
                        handlePipelineFilterStyle();
                        break;
                    default:
                        System.out.println("无效的选择，请重新输入。");
                        break;
                }
            } else {
                System.out.println("无效的选择，请重新输入。");
                scanner.next(); // 清除非整数输入
            }
        } while (true);
    }

    public static void handleMainSubprogramStyle() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要读取的文件路径：");
        String filePath = scanner.nextLine();

        String fileContent = readFile(filePath);
        if (fileContent != null) {
            List<String> lines = getLines(fileContent);
            List<String> rotatedLines = rotateKeywords(lines);
            printLines(rotatedLines);
        }
    }

    public static void handleObjectOrientedStyle() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要读取的文件路径：");
        String filePath = scanner.nextLine();

        String fileContent = readFile(filePath);
        if (fileContent != null) {
            TextProcessor textProcessor = new TextProcessor(fileContent);
            textProcessor.rotateKeywords();
            textProcessor.printLines();
        }
    }

    public static void handleEventSystemStyle() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要读取的文件路径：");
        String filePath = scanner.nextLine();

        String fileContent = readFile(filePath);
        if (fileContent != null) {
            EventBasedProcessor eventBasedProcessor = new EventBasedProcessor();
            eventBasedProcessor.readFile(fileContent);
            eventBasedProcessor.rotateKeywords();
            eventBasedProcessor.printLines();
        }
    }

    public static void handlePipelineFilterStyle() {
        Scanner scanner = new Scanner(System.in);

        System.out.print("请输入要读取的文件路径：");
        String filePath = scanner.nextLine();

        String fileContent = readFile(filePath);
        if (fileContent != null) {
            PipelineFilterProcessor pipelineFilterProcessor = new PipelineFilterProcessor(fileContent);
            pipelineFilterProcessor.process();
        }
    }

    public static String readFile(String filePath) {
        StringBuilder builder = new StringBuilder();

        try (BufferedReader reader = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.lineSeparator());
            }
        } catch (FileNotFoundException e) {
            System.out.println("无法找到指定的文件");
            return null;
        } catch (IOException e) {
            System.out.println("读取文件时发生错误");
            return null;
        }

        return builder.toString();
    }

    public static List<String> getLines(String fileContent) {
        List<String> lines = new ArrayList<>();
        Scanner scanner = new Scanner(fileContent);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
                lines.add(line);
            }
        }

        scanner.close();
        return lines;
    }

    public static List<String> rotateKeywords(List<String> lines) {
        List<String> rotatedLines = new ArrayList<>();

        for (String line : lines) {
            String[] words = line.split("\\s+");

            for (int i = 0; i < words.length; i++) {
                StringBuilder rotated = new StringBuilder();
                for (int j = i; j < words.length + i; j++) {
                    int index = j % words.length;
                    rotated.append(words[index]).append(" ");
                }
                rotatedLines.add(rotated.toString().trim());
            }
        }

        return rotatedLines;
    }

    public static void printLines(List<String> lines) {
        for (String line : lines) {
            System.out.println(line);
        }
    }
}

class TextProcessor {
    private String fileContent;

    public TextProcessor(String fileContent) {
        this.fileContent = fileContent;
    }

    public void printLines() {
        String[] lines = fileContent.split("\\n");
        for (String line : lines) {
            System.out.println(line);
        }
    }

    public void rotateKeywords() {
        List<String> lines = getLines(fileContent);
        List<String> rotatedLines = rotateKeywords(lines);
        for (String line : rotatedLines) {
            System.out.println(line);
        }
    }

    private List<String> getLines(String fileContent) {
        List<String> lines = new ArrayList<>();
        Scanner scanner = new Scanner(fileContent);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
                lines.add(line);
            }
        }

        scanner.close();
        return lines;
    }

    private List<String> rotateKeywords(List<String> lines) {
        List<String> rotatedLines = new ArrayList<>();

        for (String line : lines) {
            String[] words = line.split("\\s+");

            for (int i = 0; i < words.length; i++) {
                StringBuilder rotated = new StringBuilder();
                for (int j = i; j < words.length + i; j++) {
                    int index = j % words.length;
                    rotated.append(words[index]).append(" ");
                }
                rotatedLines.add(rotated.toString().trim());
            }
        }

        return rotatedLines;
    }
}

class EventBasedProcessor {
    private String fileContent;

    public void readFile(String fileContent) {
        this.fileContent = fileContent;
    }

    public void rotateKeywords() {
        List<String> lines = getLines(fileContent);
        List<String> rotatedLines = rotateKeywords(lines);
        printLines();
    }



    private List<String> getLines(String fileContent) {
        List<String> lines = new ArrayList<>();
        Scanner scanner = new Scanner(fileContent);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
                lines.add(line);
            }
        }

        scanner.close();
        return lines;
    }

    private List<String> rotateKeywords(List<String> lines) {
        List<String> rotatedLines = new ArrayList<>();

        for (String line : lines) {
            String[] words = line.split("\\s+");

            for (int i = 0; i < words.length; i++) {
                StringBuilder rotated = new StringBuilder();
                for (int j = i; j < words.length + i; j++) {
                    int index = j % words.length;
                    rotated.append(words[index]).append(" ");
                }
                rotatedLines.add(rotated.toString().trim());
            }
        }

        return rotatedLines;
    }

    public void printLines() {
        String[] lines = fileContent.split(System.lineSeparator());
        for (String line : lines) {
            System.out.println(line);
        }
    }



}

class PipelineFilterProcessor {
    private String fileContent;

    public PipelineFilterProcessor(String fileContent) {
        this.fileContent = fileContent;
    }

    public void process() {
        List<String> lines = getLines(fileContent);
        filterLines(lines);
    }

    private List<String> getLines(String fileContent) {
        List<String> lines = new ArrayList<>();
        Scanner scanner = new Scanner(fileContent);

        while (scanner.hasNextLine()) {
            String line = scanner.nextLine().trim();
            if (!line.isEmpty()) {
                lines.add(line);
            }
        }

        scanner.close();
        return lines;
    }

    private void filterLines(List<String> lines) {
        // 执行过滤操作，示例中直接打印输出
        for (String line : lines) {
            System.out.println(line);
        }
    }
}




